package com.kp.ai.service.function;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.kp.ai.service.CarBookingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.core.NestedExceptionUtils;
import com.kp.ai.enums.BookingStatus;
import java.time.LocalDate;
import java.util.function.Function;

@Configuration
public class CarBookingFunction {

	private static final Logger logger = LoggerFactory.getLogger(CarBookingFunction.class);

	@Autowired
	private CarBookingService carBookingService;

	public record BookingDetailsRequest(String bookingNumber, String name) {
	}

	public record ChangeBookingDatesRequest(String bookingNumber, String name, String date, String from, String to, Long carTypeId) {
	}

	public record CancelBookingRequest(String bookingNumber) {
	}

	@JsonInclude(Include.NON_NULL)
	public record BookingDetails(String bookingNumber, String name, LocalDate date, BookingStatus bookingStatus,
			String from, String to, com.kp.ai.entity.CarType carType) {
	}

	public record CreateBookingRequest(String name, String date, String from, String to, Long carTypeId, String carTypeName, String phone) {}

	@Bean
	@Description("获取订单详细信息")
	public Function<BookingDetailsRequest, BookingDetails> getBookingDetails() {
		return request -> {
			try {
				return carBookingService.getBookingDetails(request.bookingNumber(), request.name());
			}
			catch (Exception e) {
				logger.warn("CarBooking details: {}", NestedExceptionUtils.getMostSpecificCause(e).getMessage());
				return new BookingDetails(request.bookingNumber(), request.name(), null, null, null, null, null);
			}
		};
	}

	@Bean
	@Description("修改订单")
	public Function<ChangeBookingDatesRequest, String> changeBooking() {
		return request -> {
			try {
				carBookingService.changeBooking(
					request.bookingNumber(),
					request.name(),
					request.date(),
					request.from(),
					request.to(),
					request.carTypeId()
				);
				return "";
			}catch (IllegalArgumentException e){
				return e.getMessage();
			}
		};
	}

	@Bean
	@Description("取消订单")
	public Function<CancelBookingRequest, String> cancelBooking() {
		return request -> {
			try {
				carBookingService.cancelBooking(request.bookingNumber());
			return "";
			} catch (IllegalArgumentException e) {
				return e.getMessage();
			}
		};
	}

	@Bean
	@Description("创建订单")
	public Function<CreateBookingRequest, String> createBooking() {
		return request -> {
			try {
				carBookingService.createBooking(
					request.name(),
					request.date(),
					request.from(),
					request.to(),
					request.carTypeId(),
					request.carTypeName(),
					request.phone()
				);
				return "订单已成功创建！";
			} catch (Exception e) {
				return "创建订单失败：" + e.getMessage();
			}
		};
	}

}
